import 'package:flutter/material.dart';
import '../models/device_item.dart';
import '../../../../config/themes/app_colors.dart';

/// 设备卡片组件
class DeviceCard extends StatelessWidget {
  final DeviceItem device;
  final VoidCallback onToggle;
  final VoidCallback onTap;
  
  const DeviceCard({
    Key? key,
    required this.device,
    required this.onToggle,
    required this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final isOnline = device.status == 'online';
    
    return Card(
      margin: const EdgeInsets.only(bottom: 12),
      elevation: 2,
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(12),
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Row(
            children: [
              // 设备图标
              Container(
                width: 50,
                height: 50,
                decoration: BoxDecoration(
                  color: device.isOn && isOnline
                      ? AppColors.primary.withOpacity(0.1)
                      : Colors.grey.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Icon(
                  IconData(device.iconData, fontFamily: 'MaterialIcons'),
                  color: device.isOn && isOnline
                      ? AppColors.primary
                      : Colors.grey,
                  size: 30,
                ),
              ),
              
              const SizedBox(width: 16),
              
              // 设备信息
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      device.name,
                      style: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const SizedBox(height: 4),
                    Row(
                      children: [
                        Container(
                          width: 8,
                          height: 8,
                          decoration: BoxDecoration(
                            color: isOnline
                                ? AppColors.deviceOnline
                                : AppColors.deviceOffline,
                            borderRadius: BorderRadius.circular(4),
                          ),
                        ),
                        const SizedBox(width: 6),
                        Text(
                          isOnline ? '在线' : '离线',
                          style: TextStyle(
                            fontSize: 12,
                            color: isOnline
                                ? AppColors.deviceOnline
                                : AppColors.deviceOffline,
                          ),
                        ),
                        const SizedBox(width: 10),
                        Text(
                          device.type,
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.grey[600],
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              
              // 开关
              if (isOnline)
                Switch(
                  value: device.isOn,
                  onChanged: (_) => onToggle(),
                  activeColor: AppColors.primary,
                )
              else
                TextButton(
                  onPressed: null,
                  child: const Text(
                    '离线',
                    style: TextStyle(color: Colors.grey),
                  ),
                ),
            ],
          ),
        ),
      ),
    );
  }
} 